//
//  FQGradientView.m
//  GradientViewDemo
//
//  Created by issuser on 2021/12/24.
//

#import "FQGradientView.h"

@interface FQGradientView ()

@property (strong, nonatomic) CAGradientLayer * gradientLayer;

@property (strong, nonatomic) CALayer * gradientContentLayer;

@property (strong, nonatomic) UIView * contentView;

@property (strong, nonatomic) CAShapeLayer * circleLayer;


@end

@implementation FQGradientView

-(instancetype)initWithFrame:(CGRect)frame withW:(CGFloat)w color:(UIColor *)color
{
    if (self = [super initWithFrame:frame]) {
        self.borderW = w == 0 ? 30 : w;
        self.borderColor = color ? color : UIColor.blueColor;
        self.backgroundColor = UIColor.clearColor;
    }
    return self;
}

//只需要传入0.60.360这种
-(void)setProgress:(CGFloat)progress
{
    _progress = progress;
    [self getCircleValue:progress];
}

//创建渐变效果
-(void)getCircleValue:(CGFloat)progress{
    
    //开始均从0开始.结束根据进度
    CGFloat startPercentage = 0.025;
    CGFloat endPercentage = progress / 360.0 - 0.025;
    
    [self.contentView.layer addSublayer:self.gradientContentLayer];
    
    CGFloat circleWH = self.contentView.bounds.size.width;
    //整段的方案.
    [self newCircleLayer:self.circleLayer center:CGPointMake(circleWH * 0.5, circleWH * 0.5) radius:(circleWH - self.borderW) * 0.5 borderWidth:self.borderW fillColor:UIColor.clearColor borderColor:UIColor.orangeColor startPercentage:startPercentage endPercentage:endPercentage];
    [self addGradientView:self.circleLayer colors1:@[(id)self.borderColor.CGColor,(id)self.borderColor.CGColor] colors2:@[(id)UIColor.clearColor.CGColor,(id)self.borderColor.CGColor] locations1:@[@0.0,@0.6,@1.0] locations2:@[@0.0,@0.2,@1.0]];
}

//这个是整个屏幕的.
-(void)addGradientView:(CAShapeLayer *)shareLayer colors1:(NSArray *)colors1 colors2:(NSArray *)colors2 locations1:(NSArray *)locations1 locations2:(NSArray *)locations2{
    
    CGFloat circleWH = self.contentView.bounds.size.width;
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.shadowPath = shareLayer.path;
    gradientLayer.frame = CGRectMake(0, 0, circleWH * 0.5,circleWH);
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.locations = locations1;
    [gradientLayer setColors:colors1];
    
    CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.shadowPath = shareLayer.path;
    gradientLayer1.frame = CGRectMake(circleWH * 0.5, 0,circleWH * 0.5,circleWH * 0.5);
    gradientLayer1.startPoint = CGPointMake(0, 0);
    gradientLayer1.endPoint = CGPointMake(1, 1);
    gradientLayer1.locations = locations1;
    [gradientLayer1 setColors:colors2];
    
    CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
    gradientLayer2.shadowPath = shareLayer.path;
    gradientLayer2.frame = CGRectMake(circleWH * 0.5, circleWH * 0.5,circleWH * 0.5,circleWH * 0.5);
    gradientLayer2.startPoint = CGPointMake(0, 0);
    gradientLayer2.endPoint = CGPointMake(0, 1);
    gradientLayer2.locations = locations2;
    [gradientLayer2 setColors:colors1];
    
    [self.gradientContentLayer addSublayer:gradientLayer]; //设置颜色渐变
    [self.gradientContentLayer addSublayer:gradientLayer1];
    [self.gradientContentLayer addSublayer:gradientLayer2];
    [self.gradientContentLayer setMask:shareLayer];

}

-(void)setStartAngle:(CGFloat)startAngle
{
    _startAngle = startAngle;
    self.gradientContentLayer.transform = CATransform3DRotate(CATransform3DIdentity, (startAngle + M_PI_2), 0, 0, 1);
}



- (void)newCircleLayer:(CAShapeLayer*)circle
                center:(CGPoint)center
                radius:(CGFloat)radius
           borderWidth:(CGFloat)borderWidth
             fillColor:(UIColor*)fillColor
           borderColor:(UIColor*)borderColor
       startPercentage:(CGFloat)startPercentage
         endPercentage:(CGFloat)endPercentage {
    
    UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:center
                                                        radius:radius
                                                    startAngle:-M_PI_2
                                                      endAngle:M_PI_2 * 3
                                                     clockwise:YES];
    
    circle.fillColor = fillColor.CGColor;
    circle.strokeColor = borderColor.CGColor;
    circle.strokeStart = startPercentage;
    circle.strokeEnd = endPercentage;
    circle.lineWidth = borderWidth;
    circle.lineCap = kCALineCapRound;
    circle.lineJoin = kCALineJoinRound;
    circle.path = path.CGPath;
}

-(UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:self.bounds];
        [self addSubview:_contentView];
    }
    return _contentView;
}

-(CALayer *)gradientContentLayer
{
    if (!_gradientContentLayer) {
        _gradientContentLayer = [[CALayer alloc]init];
        _gradientContentLayer.frame = self.contentView.bounds;
        _gradientContentLayer.backgroundColor = UIColor.clearColor.CGColor;
    }
    return _gradientContentLayer;
}

-(CAShapeLayer *)circleLayer
{
    if (!_circleLayer) {
        _circleLayer = [[CAShapeLayer alloc]init];
    }
    return _circleLayer;
}

@end
